import deepClone from '@/libs/deepClone'
import useGlobalAPI from './useGlobalAPI'

export function useRouteQuery(option?: {queryKeys: string[]}) {
  const {route, router} = useGlobalAPI()

  const routeQuery = computed(() => deepClone(route.query))
  /**路由参数中所有的字段 */
  const routeQueryKeys = computed(() => Object.keys(routeQuery.value))

  /**检测query参数名是否存在于传入的数组中
   * @存在时返回query对象
   * @反之返回false
   */
  function getQueryData<T = any>(): undefined | T {
    const query = deepClone(route.query)

    console.log('当前页面携带参数', query)
    if (option) {
      for (let i = 0; i < option.queryKeys.length; i++) {
        const key = option.queryKeys[i]
        if (!routeQueryKeys.value.includes(key)) {
          return undefined
        }
      }
    }
    return route.query as unknown as T
  }

  return {
    route,
    router,
    getQueryData
  }
}
